﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class NamkehoachChitieuDAO: SqlDAO
    {
        public const string PROCEDURE_INSERT = "spNamKeHoachChiTieu_Insert";
        public const string PARAM_TABLE = "@Table";
        public NamkehoachChitieuDAO(string connectionString, System.Type type):
            base(connectionString, type){ }

        public bool Save(List<NamkehoachChitieu> data)
        {
            try
            {
                DataTable dataTable = GetDataTable(data);
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT);
                cmd.Parameters.Add(new SqlParameter(PARAM_TABLE, dataTable));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool Update(List<NamkehoachChitieu> data)
        {
            try
            {
                DataTable dataTable = GetDataTable(data);
                SqlCommand cmd = this.GetSprocCommand("");
                cmd.Parameters.Add(new SqlParameter(PARAM_TABLE, dataTable));

                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch 
            {
                throw;
            }
        }

        private DataTable GetDataTable(List<NamkehoachChitieu> data)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("NAM_KE_HOACH_ID");
            dt.Columns.Add("CHI_TIEU_ID");

            foreach (var item in data)
            {
                dt.Rows.Add(item.ID, item.NAM_KE_HOACH_ID, item.CHI_TIEU_ID);
            }

            return dt;
        }

    }
}
